<template>
	<view class="pathway on-pathway" ref="pathway" @touchend="onEnd" :style="{backgroundColor:forntBg}">
		<view class="tips">
			<text v-if="isOk" style="color: #FFFFFF;">{{successText}}</text>
			<text v-if="!isOk">{{initText}}</text>
		</view>
		<view class="track" :style="{backgroundColor:sliderBlockBg,'transform':'translateX('+oldx+'px)'}"></view>
		<movable-area :animation="true" class="movable-area">
			<movable-view class="on-track" ref="track" :class="{'active':isOk}" 
				:x="x" direction="horizontal" @change="onMove"
				:disabled="isOk"> 
			<!-- #ifdef APP-NVUE -->
				<image v-if="!isOk" style="width: 68rpx; height: 68rpx;margin-left: 12rpx;" src="@/static/start.png"></image>
				<image v-if="isOk" style="width: 68rpx; height: 68rpx;margin-left: 12rpx;" src="@/static/end.png"></image>
			<!-- #endif -->
			</movable-view>
		</movable-area>
	</view>
</template>

<script>
	export default {
		props:{
			initText:{
				type:String,
				default:'右滑确认送达' //未滑动的验证文案
			},
			successText:{
				type:String,
				default:'确认送达'//验证成功文案
			},
			sliderBlockBg:{//
				type:String,
				default:'#dae2d0'//滑块颜色
			},
			forntBg:{
				type:String,
				default:'#ffc400'//滑块滑动后的颜色
			}
		},
		data() {
			return {
				x: 0,
				oldx:0,
				isOk: false,
				size: {},
				count:0,
				isMove:false
			};
		},
		mounted() {
			let getSize = (selector) => {
				// #ifdef APP-NVUE
				if(selector=='.on-pathway'){
					return new Promise((resolve, reject) => {
						const dom = uni.requireNativePlugin('dom')
						dom.getComponentRect(this.$refs.pathway,({result,size})=>{
							
							resolve(size.width);
						})
					});
				}else{
					return new Promise((resolve, reject) => {
						const dom = uni.requireNativePlugin('dom')
						dom.getComponentRect(this.$refs.track,({result,size})=>{
							
							resolve(size.width);
						})
					});
				}
				
				// #endif
				// #ifndef APP-NVUE
				return new Promise((resolve, reject) => {
					let view = uni.createSelectorQuery().in(this).select(selector);
					view.fields({
						size: true,
					}, (res) => {
						resolve(res.width);
					}).exec();
				});
				// #endif
			}
			this.$nextTick(() => {
				getSize(".on-pathway").then((res1) => {
					this.size.pathway = res1;
					getSize(".on-track").then((res2) => {
						this.size.track = res2;
					});
				})
			})
		},
		methods: {
			/* 滑动中 */
			onMove(e) {
				this.oldx = e.detail.x;
			},
			/* 滑动结束 */
			onEnd() {
				if (this.isOk || this.oldx<1) {
					return;
				}
				
				this.count++;
				this.x = this.oldx;
				
				if(!this.size.pathway){
					this.size.pathway = 660
				}
				if(!this.size.track){
					this.size.track = 120
				}
				// #ifdef APP-NVUE
				let x = this.oldx + 3
				// #endif
				// #ifndef APP-NVUE
				let x = this.oldx + 3
				// #endif
				console.log(x,this.size.pathway ,this.size.track)
				if (x > (this.size.pathway - this.size.track)) {
					this.isOk = true;
					this.$emit("result",{flag:true,count:this.count});
				} else {
					this.$nextTick(() => {
						setTimeout(() => {
							this.x = 0;
							this.oldx = 0;
						}, 100);
					});
					this.$emit("result",{flag:false,count:this.count});
				}
			},
			/* 重置 */
			reset(){
				this.x = 0;
				this.oldx = 0;
				this.count = 0;
				//this.isOk = false;
				setTimeout(() => {
					this.isOk = false;
				}, 1000);
			}
		}
	}
</script>

<style scoped lang="scss">
	.pathway {
		height: 80rpx;
		width: 660rpx;
		background: #ff6700;
		position: relative;
		overflow: hidden;
		
		/* 提示信息 */
		.tips {
			position: absolute;
			top: 0;
			left: 0;
			width: 660rpx;
			height:80rpx;
			line-height: 80rpx;
			text-align: center;
			color: #666;
			font-size: 32rpx;
			z-index: 3;
			display:flex;
			justify-content: center;
			align-items: center;
		}
		
		/* 滑动轨道 */
		.track {
			position: absolute;
			top: 0;
			left: 0;
			background: #eeeeee;
			width: 660rpx;
			height: 80rpx;
			padding-left: 0;
			//box-sizing: content-box;
			transform: translateX(0);
		}
		
		.movable-area {
			position: absolute;
			top: 0;
			left: 0;
			height: 80rpx;
			width:660rpx;
			background: none;
			z-index: 5;
		}
		
		.on-track {
			/* #ifdef H5 */
			height: 75rpx;
			/* #endif */
			/* #ifndef H5 */
			height: 80rpx;
			/* #endif */
			width: 100rpx;
			//box-sizing: border-box;
			background: #fff;
			/* #ifdef APP-NVUE */
			border: #dddddd solid 1px;
			/* #endif */
			/* #ifndef APP-NVUE */
			border: #dddddd solid 1px;
			background-position: center;
			background-repeat: no-repeat;
			background-size: auto 32rpx;
			background-image: url("");
			/* #endif */
		}
		
		.on-track.active {
			/* #ifdef APP-NVUE */
			border: #ff6700 solid 1px;
			/* #endif */
			/* #ifndef APP-NVUE */
			border: #ff6700 solid 1px;
			background-image: url("");
			/* #endif */
		}
		
		/* 禁止 */
		.disabled {
			position: absolute;
			z-index: 10;
			height: 100%;
			width: 100%;
			top: 0;
			left: 0;
		}
	}
</style>